<template>
  <div class="shop-list">
    <div class="action-bar">
      <el-button type="primary" @click="handleAddShop">
        添加商店
      </el-button>
    </div>
    
    <el-table :data="shops" style="width: 100%">
      <el-table-column prop="name" label="商店名称" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="manager" label="店长" />
      <el-table-column prop="status" label="状态">
        <template #default="{ row }">
          <el-tag :type="row.status === 'active' ? 'success' : 'danger'">
            {{ row.status === 'active' ? '营业中' : '已关闭' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button-group>
            <el-button type="primary" size="small" @click="handleEditShop(row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDeleteShop(row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 添加对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑商店' : '添加商店'"
      width="500px"
    >
      <el-form :model="shopForm" :rules="rules" ref="shopFormRef" label-width="100px">
        <el-form-item label="商店名称" prop="name">
          <el-input v-model="shopForm.name" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="shopForm.address" />
        </el-form-item>
        <el-form-item label="店长" prop="manager">
          <el-input v-model="shopForm.manager" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="shopForm.status" style="width: 100%">
            <el-option label="营业中" value="active" />
            <el-option label="已关闭" value="inactive" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref ,onMounted} from 'vue'
import { shopApi } from '@/api/shop'
import { ElMessage, ElMessageBox } from 'element-plus'

onMounted(async () => {
  const res = await shopApi.getShops()
  shops.value = res.data
})
const shops = ref([
  {
    name: '示例商店1',
    address: '北京市朝阳区xxx街道',
    manager: '张三',
    status: 'active'
  },
  {
    name: '示例商店2',
    address: '上海市浦东新区xxx路',
    manager: '李四',
    status: 'inactive'
  }
])

const dialogVisible = ref(false)
const shopFormRef = ref(null)
const shopForm = ref({
  name: '',
  address: '',
  manager: '',
  status: 'active'
})

const rules = {
  name: [{ required: true, message: '请输入商店名称', trigger: 'blur' }],
  address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
  manager: [{ required: true, message: '请输入店长姓名', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

const isEdit = ref(false)
const currentShopId = ref(null)

const handleAddShop = () => {
  isEdit.value = false
  currentShopId.value = null
  dialogVisible.value = true
  shopForm.value = {
    name: '',
    address: '',
    manager: '',
    status: 'active'
  }
}

const handleEditShop = (row) => {
  isEdit.value = true
  currentShopId.value = row.id
  dialogVisible.value = true
  shopForm.value = { ...row }
}

const handleDeleteShop = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该商店吗？', '提示', {
      type: 'warning'
    })
    await shopApi.deleteShop(row.id)
    ElMessage.success('删除成功')
    // 重新加载商店列表
    const res = await shopApi.getShops()
    shops.value = res.data
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败：' + error.message)
    }
  }
}

const submitForm = async () => {
  if (!shopFormRef.value) return
  
  await shopFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (isEdit.value) {
          let params = {
            ...shopForm.value
          }
          delete params.id
          delete params.createdAt
          delete params.updatedAt
          await shopApi.updateShop(currentShopId.value, params)
          ElMessage.success('更新成功')
        } else {
          await shopApi.createShop(shopForm.value)
          ElMessage.success('添加成功')
        }
        dialogVisible.value = false
        // 重新加载商店列表
        const res = await shopApi.getShops()
        shops.value = res.data
      } catch (error) {
        ElMessage.error((isEdit.value ? '更新' : '添加') + '失败：' + error.message)
      }
    }
  })
}
</script>

<style scoped>
.shop-list {
  padding: 20px;
}

.action-bar {
  margin-bottom: 20px;
}
</style> 